<!-- 内容管理系统首页 -->
<div th:fragment="cms-index">
	<script th:inline="javascript">
		
		var checkedDocRows = [];//稿件选中数
		var currentTreeNode = {};//当前选中的栏目节点
		
		$(function() {
		    var panehHidden = false;
		    if ($(this).width() < 769) {
		        panehHidden = true;
		    }
		    //在iframe页面，layout-north隐藏
			if(self == top){
				$(".ui-layout-north").removeClass('hide');
			}else{
				$(".ui-layout-north").remove();
			}
		    $('body').layout({ "initClosed" : panehHidden, "west__size" : 185 });
		    
		    renderChannelTree();
		    
		});
		
		//渲染左侧栏目树
		function renderChannelTree() {
			var url = ctx + "cms/channel/treeData";
			var options = {
		        url: url,
		        expandLevel: 1,
		        onClick: function (event, treeId, treeNode) {
					channelTreeOnClick(treeNode);
				},
				done: function(tree, data){
					if(currentTreeNode && currentTreeNode.id){
						tree.selectNode(currentTreeNode);
					}else{
						var treeNode = tree.getNodesByParam("level", "0", null)[0];
				    	tree.selectNode(treeNode);
				    	currentTreeNode = treeNode;
					}
					channelTreeOnClick(currentTreeNode);
				}
		    };
			$.tree.init(options);
		}
		
		//栏目树点击事件
		function channelTreeOnClick(treeNode){
			currentTreeNode = treeNode;//当前选中的栏目节点
			$('#ui-cms-content').empty();
    		$('#ui-cms-toolbar').empty();
    		if(!treeNode.pId || treeNode.pId == 'root'){
    			$('#site-channel-config').find('span').text('站点配置');
    			$("#channelContainerTpl").tmpl(treeNode).appendTo('#ui-cms-content');
    			$("#siteOperateTpl").tmpl(treeNode).appendTo('#ui-cms-toolbar');
    			
    			$("#channelId").val(treeNode.id);//栏目编号
				$("#parentId").val(treeNode.pId);//父栏目编号
				$("#siteId").val(treeNode.siteId);//站点编号
			
    			queryChannelList();
    		}else{
    			$('#site-channel-config').find('span').text('栏目配置');
    			$("#documentContainerTpl").tmpl(treeNode).appendTo('#ui-cms-content');
    			$("#channelOperateTpl").tmpl(treeNode).appendTo('#ui-cms-toolbar');
    			
    			$("#channelId").val(treeNode.id);//栏目编号
				$("#parentId").val(treeNode.pId);//父栏目编号
				$("#siteId").val(treeNode.siteId);//站点编号
				
    			queryDocumentList();
    		}
		}
		
		$('#btnExpand').click(function() {
			$._tree.expandAll(true);
		    $(this).hide();
		    $('#btnCollapse').show();
		});
		
		$('#btnCollapse').click(function() {
			$._tree.expandAll(false);
		    $(this).hide();
		    $('#btnExpand').show();
		});
		
		$('#btnRefresh').click(function() {
			renderChannelTree();
		});
		
		//栏目列表
		function queryChannelList() {
			var prefix = ctx + "cms/channel";
		    var options = {
		        code: "id",
		        parentCode: "parentId",
		        uniqueId: "id",
		        url: prefix + "/list",
		        createUrl: prefix + "/add/{id}",
		        updateUrl: prefix + "/edit/{id}",
		        removeUrl: prefix + "/remove/{id}",
		        modalName: "栏目",
		        ajaxParams: {"siteId" : $("#siteId").val()},
		        expandAll: false,
		        columns: [{
                    field: 'selectItem', 
                    radio: true
                 },
                 {
		            field: 'channelName',
		            title: '栏目名称',
		            width: '30%'
		        },
		        {
		            field: 'orderNum',
		            title: '显示顺序',
		            width: '8%'
		        },
		        {
		            field: 'status',
		            title: '状态',
		            width: '5%'
		        },
		        {
		            field: 'remark',
		            title: '备注',
		            width: '15%'
		        },
		        {
		            field: 'createTime',
		            title: '创建时间',
		            width: '17%'
		        },
		        {
		            title: '操作',
		            width: '20%',
		            formatter: function(value, row, index) {
		            	return $('#operateTpl').tmpl(row).html();
		            }
		        }]
		    };
		    $.treeTable.init(options);
		};

		//稿件列表
		function queryDocumentList() {
			var prefix = ctx + "cms/document";
			var channelId = $('#channelId').val();
		    var options = {
		        url: prefix + "/list",
		        createUrl: prefix + "/add/" + channelId,
		        updateUrl: prefix + "/edit/{id}",
		        removeUrl: prefix + "/remove",
		        exportUrl: prefix + "/export",
		        importUrl: prefix + "/importData",
		        importTemplateUrl: prefix + "/importTemplate",
		        queryParams: function(params){
		        	params.channelId = channelId;
		        	return params;
		        },
		        sortName: "createTime",
		        sortOrder: "desc",
		        modalName: "稿件",
		        columns: [{
		            checkbox: true
		        },
		        {
		            field: 'id',
		            visible: false
		        },
		        {
		            field: 'title',
		            title: '稿件标题',
		            width: '50%',
		            sortable: true,
		            formatter: function(value, row, index) {
		            	return '<a onclick="$.operate.editTab(\'' + row.id + '\')">' + row.title + '</a>';
		            }
		        },
		        {
		            field: 'docAuthor',
		            title: '稿件作者',
		            align: 'center',
		        },
		        {
		            field: 'channelName',
		            title: '所在栏目'
		        },
		        {
		        	field: 'status',
		        	title: '稿件状态',
		        	align: 'center'
		        },
		        {
		            field: 'createTime',
		            title: '创建时间',
		            sortable: true
		        }],
		        onCheckAll: function(rows){
		        	tableCheckListioner(rows, true);
		        },
		        onUncheckAll: function(rows){
		        	tableCheckListioner(rows, false);
		        },
		        onCheck: function(row){
		        	tableCheckListioner([row], true);
		        },
		        onUncheck:function(row){
		        	tableCheckListioner([row], false);
		        } 
		    };
		    $.table.init(options);
		}
		
		//监听table的checkbox事件操作
		function tableCheckListioner(rows, checked){
			for(var k = 0; k < rows.length; k++){
        		var row = rows[k];
        		var result = false;
	        	for(var i = 0; i < checkedDocRows.length; i++){
	        		if(checkedDocRows[i].id == row.id){
	        			result = true;
	        			break;
	        		}
	        	}
	        	if(checked && !result){
		        	checkedDocRows.push(row);
	        	}else{
	        		checkedDocRows.splice(i, 1);
	        	}
	        	
	        	if(checkedDocRows && checkedDocRows.length == 1){
	        		$('#ui-cms-toolbar').find('li.single').removeClass('hide');
	        		$('#ui-cms-toolbar').find('li.multiple').removeClass('hide');
	        	}else if(checkedDocRows && checkedDocRows.length > 1){
	        		$('#ui-cms-toolbar').find('li.single').addClass('hide');
	        		$('#ui-cms-toolbar').find('li.multiple').removeClass('hide');
	        	}else{
	        		$('#ui-cms-toolbar').find('li.single').addClass('hide');
	        		$('#ui-cms-toolbar').find('li.multiple').addClass('hide');
	        	}
        	}
		}
		
		//站点栏目配置
		function siteChannelConfig(){
			if(!currentTreeNode.pId || currentTreeNode.pId == 'root'){//站点
				$.modal.open('站点配置', ctx + 'cms/site/config/edit/' + currentTreeNode.siteId);
			}else{//栏目
			
			}
		}
		
		//修改这篇稿件
		function editDocument(){
			if(checkedDocRows && checkedDocRows.length == 1){
				$.modal.openTab('修改稿件', ctx + 'cms/document/edit/' + checkedDocRows[0].id);
			}
		}
		
		//批量新增栏目
		function batchChannel(){
			$.modal.open('批量新增栏目', ctx + 'cms/channel/batch/' + currentTreeNode.id);
		}

	</script>
</div>